<template>
  <!-- 新增弹层 -->
  <el-dialog
    :title="FormData.id ? '编辑角色' : '新增角色'"
    :visible="dialogVisible"
    @close="closeDialog"
    @open="openDialog"
  >
    <el-form ref="form" label-width="80px" :model="FormData" :rules="rules">
      <el-form-item label="文章标题:" prop="title" label-width="100px">
        <el-input v-model="FormData.title" placeholder="请输入文章标签"  />
      </el-form-item>
      <el-form-item label="文章内容:" prop="articleBody" label-width="100px">
        <!-- 富文本编辑器 -->
        <quill-editor
          :options="editorOption"
          v-model="FormData.articleBody"
          @blur="contentRule"
          style="heght: 100px"
        ></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址:" prop="description" label-width="100px">
        <el-input v-model="FormData.videoURL" placeholder="请输入视频地址" />
      </el-form-item>
      <el-form-item style="text-align: right">
        <el-button  @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="confirm">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
// require styles
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { add, update, detail } from '@/api/hmmm/articles.js'

const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  ['blockquote', 'code-block'], // 引用 代码块-----['blockquote', 'code-block']
  ['image', 'link'] // 链接、图片、视频-----['link', 'image', 'video']
]
export default {
  data () {
    return {
      // 富文本编辑器的配置
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions
          }
        }
      },
      FormData: {
        title: '',
        articleBody: '',
        videoURL: ''
        // id: null
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['blur'] }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: ['blur', 'change'] }
        ]
      }
      // formtitle: ''
    }
  },
  components: {
    quillEditor
  },
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    }
  },
  mounted () {
    // this.preview()
  },
  methods: {
    // 弹窗
    closeDialog () {
      this.$emit('closeDialog')
      this.$refs.form.resetFields() // 清理当前数据
      this.resform()
    },
    openDialog () {
      console.log(this.FormData)
    },
    // 兜底校验
    confirm () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.FormData.id) {
            console.log(this.FormData)
            await update(this.FormData)
            this.$message.success('修改文章成功')
          } else {
            await add(this.FormData)
            this.$message.success('新增文章成功')
          }
          // this.getlist()
          this.$emit('getlist')
          this.$emit('closeDialog')
          this.$refs.form.resetFields() // 清理当前数据
          this.resform()
        }
      })
    },
    resform () {
      this.FormData = {
        title: '',
        articleBody: '',
        videoURL: ''
        // id: null
      }
    },
    // 回填文章
    async preview (data) {
      // this.$emit('openadddialog')
      const res = await detail(data)
      console.log(res)
      this.FormData = res.data
    },
    // 富文本校验
    contentRule () {
      this.$refs.form.validateField('articleBody')
    }
  }
}

</script>

<style scoped lang='scss'>
.el-form-item::v-deep .ql-editor {
  height: 200px;
}
.el-form-item::v-deep .ql-toolbar.ql-snow {
  height: 38px;
  padding: 0px;
  padding-left: 8px;
}

</style>
